//
//  THJNewsVC.m
//  THJXHW
//
//  Created by 河南德艺 on 17/3/7.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJNewsVC.h"
#import <MJRefresh.h>
#import "THJDataBannerCell.h"
#import "THJSArticleCell.h"
#import "THJNewsDetailVC.h"
#import "ZBNetworking.h"
#import "THJZiXunInfoCell.h"
#import "THJDianJiaVC.h"

@interface THJNewsVC ()<UITableViewDelegate,UITableViewDataSource,THJDataBannerCellDelegate>
{
    UITableView *_tableview;
    NSInteger pageNO;
    NSString * typeStr;
}
@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property (weak, nonatomic) IBOutlet UIButton *btn3;
@property (strong, nonatomic) NSMutableArray *listArr;
@property (strong, nonatomic) NSMutableArray *lunboArr;
@property (strong, nonatomic) NSMutableArray *lunboInfoArr;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headViewHeight;
@end

@implementation THJNewsVC

-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
    [super viewWillAppear:animated];
    [self setStateBarColor:COLOR(83, 84, 85, 1)];
//    [[LBTabBarController sharedTabBarC].fabuView removeFromSuperview];
    [self.navigationController.navigationBar setHidden:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
//    [[LBTabBarController sharedTabBarC].fabuView removeFromSuperview];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if(KIsiPhoneX){
        self.topSpace.constant = 45;
        self.headViewHeight.constant = 85;
    }
    pageNO = 1;
    [self setTableview];
    typeStr = @"21";
    [self getMainDataWithTypeId:@"21" Page:1];
}
-(NSMutableArray*)lunboArr{
    if(!_lunboArr){
        _lunboArr = [[NSMutableArray alloc] init];
    }
    return _lunboArr;
}
-(NSMutableArray*)listArr{
    if(!_listArr){
        _listArr = [[NSMutableArray alloc] init];
    }
    return _listArr;
}
-(void)getMainDataWithTypeId:(NSString*)typeId Page:(NSInteger)page{
    __weak typeof(self) weekself = self;
    NSString * urlstr = [NSString stringWithFormat:@"%@index.php?f=article&r=article&type=%@&page=%ld",THJUrlDomain,typeId,page];
    NSLog(@"资讯url-%@",urlstr);
    [self SVHUDShow];
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString=urlstr;
        request.apiType=ZBRequestTypeRefresh;
    }  success:^(id responseObject,apiType type){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        [_tableview.mj_header endRefreshing];
        [weekself SVHUDHiden];
        if([receiveData[@"status"] isEqual:@"200"]){
            if(page == 1){
                [weekself.lunboArr removeAllObjects];
                self.lunboInfoArr = receiveData[@"data"][@"lun"];
                for (NSDictionary * dic in receiveData[@"data"][@"lun"]) {
                    [weekself.lunboArr addObject:dic[@"thumb"]];
                }
            }
            if([typeId isEqual:typeStr]){
                for (NSDictionary * dic in receiveData[@"data"][@"data"]) {
                    [weekself.listArr addObject:dic];
                }
            }else{
                weekself.listArr = receiveData[@"data"][@"data"];
            }
        }
        typeStr = typeId;
        [_tableview reloadData];
        NSLog(@"资讯Arr-%@",weekself.listArr);
    } failed:^(NSError *error){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"error-%@",error);
    }];
}
#pragma mark -----tableview相关------
-(void)setTableview{
    CGFloat constant;
    if(KIsiPhoneX){
        constant = 85;
    }else{
        constant = 65;
    }
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, constant, ScreenWidth, ScreenHeight-64-49) style:UITableViewStylePlain];
    _tableview.tableFooterView = [[UIView alloc] init];
    [_tableview registerNib:[UINib nibWithNibName:@"THJDataBannerCell" bundle:nil] forCellReuseIdentifier:@"banner"];
     [_tableview registerNib:[UINib nibWithNibName:@"THJSArticleCell" bundle:nil] forCellReuseIdentifier:@"article"];
     [_tableview registerNib:[UINib nibWithNibName:@"THJZiXunInfoCell" bundle:nil] forCellReuseIdentifier:@"text"];
    _tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    _tableview.showsVerticalScrollIndicator = NO;
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [self.view addSubview:_tableview];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if([typeStr isEqual:@"30"]){
        return 75;
    }else{
        if(indexPath.row == 0){
            return (ScreenWidth - 30)/2.3+30;
        }else{
            return 130;
        }
    }
   
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if([typeStr isEqual:@"30"]){
        return self.listArr.count;
    }else{
        return self.listArr.count+1;
    }
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if([typeStr isEqual:@"30"]){
        NSDictionary * dic = self.listArr[indexPath.row];
        THJZiXunInfoCell * cell = [tableView dequeueReusableCellWithIdentifier:@"text" forIndexPath:indexPath];
        cell.titleLab.textColor = cell.timeLab.textColor =  COLOR(71, 71, 71, 1);
        cell.titleLab.text = dic[@"title"];
        cell.timeLab.text = dic[@"addtime"];
        cell.contentView.backgroundColor = SingleColor(whiteColor);
        return cell;
    }else{
        if(indexPath.row == 0){
            THJDataBannerCell * cell = [tableView dequeueReusableCellWithIdentifier:@"banner" forIndexPath:indexPath];
            [cell setBannerWithImgUrlArr:self.lunboArr Height:(ScreenWidth - 30)/2.3+30];
            cell.delegate = self;
            return cell;
        }else{
            NSDictionary * dic = self.listArr[indexPath.row-1];
            THJSArticleCell * cell = [tableView dequeueReusableCellWithIdentifier:@"article" forIndexPath:indexPath];
            cell.titleLab.text = dic[@"title"];
            cell.timeLab.text = dic[@"addtime"];
            [cell.imgV sd_setImageWithURL:[NSURL URLWithString:dic[@"thumb"]] placeholderImage:kGetImage(@"占位图")];
            return cell;
        }
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击cell");
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic;
    if([typeStr isEqual:@"30"]){
        dic = self.listArr[indexPath.row];
    }else{
        dic = self.listArr[indexPath.row - 1];
    }
    THJNewsDetailVC * vc = [THJNewsDetailVC new];
    vc.itemid = dic[@"itemid"];
    vc.url = [NSString stringWithFormat:@"http://bigdata.thjb.net/dist/#/information/phoneinfodetail/%@/%@",typeStr,dic[@"itemid"]];
    vc.newsTitle = dic[@"title"];
    vc.newsContent = dic[@"introduce"];
    vc.mid = typeStr;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark -----轮播图点击------
-(void)adImgSelectIndex:(NSInteger)index{
//    THJDianJiaVC * vc = [THJDianJiaVC new];
//    [self.navigationController pushViewController:vc animated:YES];
    
    NSDictionary * dic = self.lunboInfoArr[index];
    THJNewsDetailVC * vc = [THJNewsDetailVC new];
    vc.itemid = dic[@"itemid"];
    vc.url = [NSString stringWithFormat:@"http://bigdata.thjb.net/dist/#/information/phoneinfodetail/%@/%@",typeStr,dic[@"itemid"]];
    vc.newsTitle = dic[@"title"];
    vc.newsContent = @"下载铁合金现货网APP获取更多行业资讯";
    vc.mid = typeStr;
    [self.navigationController pushViewController:vc animated:YES];
    NSLog(@"轮播图点击");
    
}
#pragma mark -----上拉加载下拉刷新触发方法------
-(void)loadMore{
    pageNO++;
    [self getMainDataWithTypeId:typeStr Page:pageNO];
    [_tableview.mj_footer endRefreshing];
}
#pragma mark -----行情 专题 招标------

- (IBAction)btn1Action:(UIButton*)sender {
    [self setBtnSelectColorBtn:sender];
    if(![typeStr isEqual:@"21"]){
        [self getMainDataWithTypeId:@"21" Page:pageNO];
    }
}
- (IBAction)btn2Action:(UIButton*)sender {
    [self setBtnSelectColorBtn:sender];
    if(![typeStr isEqual:@"spec"]){
        [self getMainDataWithTypeId:@"spec" Page:pageNO];
    }
}
- (IBAction)btn3Action:(UIButton*)sender {
    [self setBtnSelectColorBtn:sender];
    if(![typeStr isEqual:@"30"]){
        [self getMainDataWithTypeId:@"30" Page:pageNO];
    }
}
-(void)setBtnSelectColorBtn:(UIButton*)btn{
    [self.btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.btn3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
   [btn setTitleColor:COLOR(252, 96, 61, 1) forState:UIControlStateNormal];
}
@end
